package com.woniuxy.project115.kaer.tenant.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.project115.kaer.commons.util.JwtUtil;
import com.woniuxy.project115.kaer.commons.util.Result;
import com.woniuxy.project115.kaer.tenant.dto.SettledApplicationDto;
import com.woniuxy.project115.kaer.tenant.entity.SettledApplication;
import com.woniuxy.project115.kaer.tenant.form.ApproveApplicationForm;
import com.woniuxy.project115.kaer.tenant.form.SubmitApplicationForm;
import com.woniuxy.project115.kaer.tenant.param.ApproveApplicationParam;
import com.woniuxy.project115.kaer.tenant.param.SubmitApplicationParam;
import com.woniuxy.project115.kaer.tenant.service.SettledApplicationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author HuH
 * @since 2025年02月20日
 * 入驻申请接口
 */
@RestController
@RequestMapping("/settledApplication")
@Api(tags = "入驻申请接口")
public class SettledApplicationController {
    @Resource
    private SettledApplicationService settledApplicationService;

    @PostMapping("/uploadLogo")
    @ApiOperation("商家上传店铺logo")
    @ApiImplicitParam(name = "file", value = "店铺logo")
    public Result<String> uploadLogo(MultipartFile file) throws Exception {
        return Result.succeed("上传成功", settledApplicationService.uploadLogo(file));
    }

    @PostMapping("/submitApplication")
    @ApiOperation("商家提交入驻申请")
    public Result submitApplication(
            @RequestPart("form") SubmitApplicationForm form,
            @RequestPart(value = "files", required = false) MultipartFile[] files,
            @RequestHeader String token) throws Exception {
        System.out.println(form);
        form.setFiles(files);
        Integer tenantInfoId = JwtUtil.getTenantInfoId(token);
        settledApplicationService.submitApplication(BeanUtil.toBean(form, SubmitApplicationParam.class), tenantInfoId);
        return Result.succeed("提交成功");
    }

    @PostMapping("/getPendingApplication")
    @ApiOperation("平台获取所有的待审核入驻申请")
    public Result<SettledApplicationDto> getPendingApplication() throws Exception {
        return Result.succeed("获取成功", null, settledApplicationService.getPendingApplication());
    }

    @PostMapping("/approveApplication")
    @ApiOperation("平台审核入驻申请")
    public Result approveApplication(@RequestBody ApproveApplicationForm form) throws Exception {
        settledApplicationService.approveApplication(BeanUtil.toBean(form, ApproveApplicationParam.class));
        return Result.succeed("审核成功");
    }

    @PostMapping("/getApplication")
    @ApiOperation("商家获取自己的入驻申请")
    public Result<SettledApplication> getApplication(@RequestHeader String token) throws Exception {
        Integer tenantInfoId = JwtUtil.getTenantInfoId(token);
        return Result.succeed("获取成功", null, settledApplicationService.getApplication(tenantInfoId));
    }
}

